/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PhysicCollection;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author danh.ngothanh
 */
public class HomingMissile/* extends PhysicObject*/{    
    private double max_left_rotate_angle = 0;
    private double max_right_rotate_angle = 0;
    private double temp_target_angle = 0;
    private double missileangletorotate = 0;
    private double missiletargetangle = 0;
    private double missilecurrentangle = 0;
    private int missileposx;
    private int missileposy;
    private static final int S_MISSILE_MIN_SPEED = 1;
    private int missilespeed = S_MISSILE_MIN_SPEED;
    private double missilevelocityx = 0;
    private double missilevelocityy = 0;
    private int targetposx = 0;
    private int targetposy = 0;
    private int missilespeeddelaytime = 50;
    private int missile_type;

    private static final int TYPE_NORMAL = 0;
    private static final int TYPE_KALSIDDON = 1;
    private static final int TYPE_TOUHOU = 2;
    private static final int TYPE_OTHER = 3;

    private static final int MISSILE_MAX_SPEED = 5;
    private static final double MISSILE_MAX_ROTATE_ANGLE = Math.toRadians(1);

    public HomingMissile(int  missileposx, int missileposy, int targetposx, int targetposy, int missile_type){
        this.targetposx = targetposx;
        this.targetposy = targetposy;
        this.missileposx = missileposx;
        this.missileposy = missileposy;
        this.missile_type = missile_type;
        if(missile_type == TYPE_KALSIDDON){
            missilespeed = MISSILE_MAX_SPEED;
        }
    }
    public int getMissilePosX(){
        return missileposx;
    }
    public int getMissilePosY(){
        return missileposy;
    }
    public void setMissilePosX(int missileposx){
        this.missileposx = missileposx;
    }
    public void setMissilePosY(int missileposy){
        this.missileposy = missileposy;
    }
    public double getMissileCurrentAngle(){
        return missilecurrentangle;
    }
    public void setMissileCurrentAngle(double newmissileangle){
        this.missilecurrentangle = newmissileangle;
    }            
    public double getMissileTargetAngle(){
        return missiletargetangle;
    }
    public void setMissileType(int missile_type){
        this.missile_type = missile_type;
    }
    public int getMissileType(){
        return missile_type;
    }

    public void setMissileSpeed(int newspeed){
        missilespeed = newspeed;
        if(missilespeed > MISSILE_MAX_SPEED){
            missilespeed = MISSILE_MAX_SPEED;
        }
    }
    public int getMissileSpeed(){
        return missilespeed;
    }

    @Override
    public void finalize(){
        max_left_rotate_angle = 0;
        max_right_rotate_angle = 0;
        temp_target_angle = 0;
        missileangletorotate = 0;
        missiletargetangle = 0;
        missilecurrentangle = 0;
        missileposx = 0;
        missileposy = 0;
        missilespeed = 0;
        missilevelocityx = 0;
        missilevelocityy = 0;
        targetposx = 0;
        targetposy = 0;
        try {
            super.finalize();
        } catch (Throwable ex) {
            Logger.getLogger(HomingMissile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean isHitTarget(int targetposx, int targetposy, int target_radius){
        if((missileposx > (targetposx - target_radius)) && (missileposx < (targetposx + target_radius)) && (missileposy > (targetposy - target_radius)) && (missileposy < (targetposy + target_radius))){
            return true;
        }
        return false;
    }
    
    public void relocation(int targetposx, int targetposy){
        missiletargetangle = Math.atan2(targetposx - missileposx, targetposy - missileposy);
        missiletargetangle = Math.toRadians(180) - missiletargetangle;
        max_left_rotate_angle = missilecurrentangle - Math.toRadians(180);
        max_right_rotate_angle = missilecurrentangle + Math.toRadians(180);
        temp_target_angle = missiletargetangle;
        if(missiletargetangle <= max_left_rotate_angle){
            temp_target_angle = missiletargetangle + Math.toRadians(360);
        }
        if(missiletargetangle > max_right_rotate_angle){
            temp_target_angle = missiletargetangle - Math.toRadians(360);
        }
        missileangletorotate = temp_target_angle - missilecurrentangle;
        if(missileangletorotate > MISSILE_MAX_ROTATE_ANGLE)
            missileangletorotate = MISSILE_MAX_ROTATE_ANGLE;
        if(missileangletorotate < -MISSILE_MAX_ROTATE_ANGLE)
            missileangletorotate = -MISSILE_MAX_ROTATE_ANGLE;

        missilecurrentangle += missileangletorotate;
        if(missilecurrentangle < 0)
            missilecurrentangle += Math.toRadians(360);
        if(missilecurrentangle > Math.toRadians(360))
            missilecurrentangle -= Math.toRadians(360);  
        if(missilespeeddelaytime > 0){
            missilespeeddelaytime --;
        }else{
            missilespeeddelaytime = 1;
            if(missile_type == TYPE_NORMAL){
                missilespeed ++;
                if(missilespeed >= MISSILE_MAX_SPEED){
                    missilespeed = MISSILE_MAX_SPEED;
                }
            }else if(missile_type == TYPE_KALSIDDON){
                missilespeed -= 1;
                if(missilespeed < 3)
                    missilespeed = 3;
//                missilespeed ++;
//                if(missilespeed >= MISSILE_MAX_SPEED){
//                    missilespeed = MISSILE_MAX_SPEED;
//                }
            }
        }
        missilevelocityx = missilespeed * Math.sin(missilecurrentangle);
        missilevelocityy = missilespeed * Math.cos(missilecurrentangle);
        this.missileposx += (int)missilevelocityx;
        this.missileposy -= (int)missilevelocityy;        
    }
}
